<?php

namespace app\admin\controller;

use app\common\model\Hetong as HetongModel;
use app\common\model\User;

class Hetong extends Base
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['yiname']) {
                $where['yiname'] = ['like', "%{$data['yiname']}%"];
            }
            if ($data['idcard']) {
                $where['idcard'] = ['like', "%{$data['idcard']}%"];
            }
            if ($data['uid']) {
                $where['uid'] = ['eq', $data['uid']];
            }
            $list = HetongModel::getPage($where, $page, $size);
            if ($list)
                foreach ($list as $k => $v) {
                    $list[$k]['nickname'] = User::getValue(['id' => ['eq', $v['uid']]], 'nickname');
                }
            $total = HetongModel::getCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    /**
     * 编辑添加
     * @return \think\response\Json
     */
    public function money_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['sid']) {
                return returnJson(202, '请填写队员id');
            }
            if (!$data['pay_time']) {
                return returnJson(202, '请填写缴费日期');
            }
            if (!$data['pay_money']) {
                return returnJson(202, '请填写缴费金额');
            }
            $save_data = [
                'sid' => $data['sid'],
                'month' => $data['month'] ? $data['month'] : 6,
                'send_month' => $data['send_month'] ? $data['send_month'] : 0,
                'pay_time' => strtotime($data['pay_time']),
                'lose_time' => strtotime('+' . $data['month'] + $data['send_month'] . 'month', strtotime($data['pay_time'])),
                'pay_money' => $data['pay_money'],
                'update_time' => time(),
                'tuijian' => $data['tuijian']
            ];
            if ($data['id']) {
                $where = [
                    'id' => $data['id']
                ];
                $res = MoneyService::S()->querySave($save_data, $where);
            } else {
                $save_data['create_time'] = time();
                $res = MoneyService::S()->querySave($save_data);
            }
            if ($res) {
                return returnJson(200, '编辑成功');
            } else {
                return returnJson(202, '编辑失败');
            }
        }
    }


    /**
     * 删除
     * @return \think\response\Json
     */
    public function del_money()
    {
        $input = input();
        if (!$input['id']) {
            return returnJson(1, 'error');
        }
        $this->moneyModel->where('id', $input['id'])->delete();
        return returnJson(0, '删除成功');
    }


}

